/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
};

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return;
  return window.localStorage.getItem(name);
};

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
};

//===========对象相关处理=========

//深拷贝函数
export const cloneObject = obj => {
  if (typeof obj === "object") {
    if (isArray(obj)) {
      var newArr = [];
      for (var i = 0; i < obj.length; i++) newArr.push(obj[i]);
      return newArr;
    } else {
      var newObj = {};
      for (var key in obj) {
        newObj[key] = this.cloneObject(obj[key]);
      }
      return newObj;
    }
  } else {
    return obj;
  }
};

//判断是否为数组,该方法可以跨页面使用
export const isArray = function(o) {
  return Object.prototype.toString.call(o) == "[object Array]";
};

//判断一个字符串是否为空
export const checkNullString = function(value) {
  if (typeof value == "undefined" || value == null) {
    return "";
  }

  return String(value);
};

//===========web相关========

//获取url的参数 add by hamnet
export const getUrlKey = name => {
  var reg = new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)");
  var result = reg.exec(location.href);
  //console.log("正则结果:"+result);
  if (result == null) return null;

  return decodeURIComponent((result || [""])[1].replace(/\+/g, "%20")) || null;
};

//===========业务逻辑相关========

//element upload filelist 转换成url字符串数组
export const convertUrlArrayFromFilelist = arr => {
  var urlArr = [];
  arr.forEach(item => {
    urlArr.push(item.url);
  });

  return urlArr;
};

// url字符串数组 转换成 element upload filelist
export const convertFilelistFromUrlArray = arr => {
  var fileArr = [];
  arr.forEach(item => {
    var f = { url: item };
    fileArr.push(f);
  });

  return fileArr;
};
